#include "MyQtAndCProjectWithVS.h"
#include "opencv2\core.hpp"
#include "opencv2\imgcodecs.hpp"
#include "opencv2\highgui.hpp"
#include <opencv2/opencv.hpp>
#include <qdebug.h>
#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp> 

using namespace cv;
int thresholdValue1 = 270;//滞后性阈值1
int kenelValue = 4;//降噪内核值
const static int MaxKenelValueCanny = 10;
const static int MaxThresholdValue1 = 300;

inline QImage cvMat_to_QImage(const cv::Mat& mat) {
    switch (mat.type())
    {
        // 8-bit, 4 channel
    case CV_8UC4:
    {
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB32);
        return image;
    }

    // 8-bit, 3 channel
    case CV_8UC3:
    {
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }

    // 8-bit, 1 channel
    case CV_8UC1:
    {
        static QVector<QRgb>  sColorTable;
        // only create our color table once
        if (sColorTable.isEmpty())
        {
            for (int i = 0; i < 256; ++i)
                sColorTable.push_back(qRgb(i, i, i));
        }
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
        image.setColorTable(sColorTable);
        return image;
    }

    default:
        qDebug("Image format is not supported: depth=%d and %d channels\n", mat.depth(), mat.channels());
        break;
    }
    return QImage();
}
inline QPixmap cvMatToQPixmap(const cv::Mat& inMat)
{
    return QPixmap::fromImage(cvMat_to_QImage(inMat));
}
inline cv::Mat QImageToCvMat(const QImage& inImage, bool inCloneImageData = true)
{
    switch (inImage.format())
    {
        // 8-bit, 4 channel
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
    {
        cv::Mat  mat(inImage.height(), inImage.width(),
            CV_8UC4,
            const_cast<uchar*>(inImage.bits()),
            static_cast<size_t>(inImage.bytesPerLine())
        );

        return (inCloneImageData ? mat.clone() : mat);
    }

    // 8-bit, 3 channel
    case QImage::Format_RGB32:
    case QImage::Format_RGB888:
    {
        if (!inCloneImageData)
        {
            qWarning() << "CVS::QImageToCvMat() - Conversion requires cloning because we use a temporary QImage";
        }

        QImage   swapped = inImage;

        if (inImage.format() == QImage::Format_RGB32)
        {
            swapped = swapped.convertToFormat(QImage::Format_RGB888);
        }

        swapped = swapped.rgbSwapped();

        return cv::Mat(swapped.height(), swapped.width(),
            CV_8UC3,
            const_cast<uchar*>(swapped.bits()),
            static_cast<size_t>(swapped.bytesPerLine())
        ).clone();
    }

    // 8-bit, 1 channel
    case QImage::Format_Indexed8:
    {
        cv::Mat  mat(inImage.height(), inImage.width(),
            CV_8UC1,
            const_cast<uchar*>(inImage.bits()),
            static_cast<size_t>(inImage.bytesPerLine())
        );

        return (inCloneImageData ? mat.clone() : mat);
    }

    default:
        qWarning() << "CVS::QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
        break;
    }

    return cv::Mat();
}
inline cv::Mat QPixmapToCvMat(const QPixmap& inPixmap, bool inCloneImageData = true)
{
    return QImageToCvMat(inPixmap.toImage(), inCloneImageData);
}

MyQtAndCProjectWithVS::MyQtAndCProjectWithVS(QWidget* parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

//导入图片
void MyQtAndCProjectWithVS::openImage()
{
    filepath = QFileDialog::getOpenFileName(nullptr, QStringLiteral("选择图片"), ".", "*.png;*.jpg");
    if (filepath.trimmed().isEmpty() == false)
    {
        currentimage.load(filepath);
        currentimage = currentimage.scaled(ui.label->width(), ui.label->height());
        ui.label->setPixmap(QPixmap::fromImage(currentimage));

    }
}

//原图
void MyQtAndCProjectWithVS::button1()
{

    if (filepath.trimmed().isEmpty() == false)
    {
        currentimage.load(filepath);
        currentimage = currentimage.scaled(ui.label->width(), ui.label->height());
        ui.label->setPixmap(QPixmap::fromImage(currentimage));
    }
}

//直方图均衡化
void MyQtAndCProjectWithVS::button2() 
{
    //载入原始图
    Mat srcImage2 = QPixmapToCvMat(QPixmap::fromImage(currentimage));
    Mat dstImage2, grayImage2, dst[3];
    QPixmap dstImages6;
    //分离通道
    split(srcImage2, dst);
    //对RGB三通道各进行直方图均衡化
    equalizeHist(dst[0], dst[0]);
    equalizeHist(dst[1], dst[1]);
    equalizeHist(dst[2], dst[2]);
    //合并三通道
    merge(dst, 3, dstImage2);
    //显示效果图
    dstImages6 = cvMatToQPixmap(dstImage2);
    dstImages6 = dstImages6.scaled(ui.label->width(), ui.label->height());
    ui.label->setPixmap(dstImages6);
}


//均值滤波
void MyQtAndCProjectWithVS::button4()
{
    //载入原始图
    Mat image1 = QPixmapToCvMat(QPixmap::fromImage(currentimage));
    Mat image2;
    QPixmap image3;
    //均值模糊
    blur(image1, image2, Size(20, 20));
    //显示效果图
    image3 = cvMatToQPixmap(image2);
    image3 = image3.scaled(ui.label->width(), ui.label->height());
    ui.label->setPixmap(image3);
}

//中值滤波
void MyQtAndCProjectWithVS::button5()
{
    //载入原始图
    Mat srcImage1 = QPixmapToCvMat(QPixmap::fromImage(currentimage));
    Mat image2s;
    QPixmap image3s;
    //中值模糊
    medianBlur(srcImage1, image2s, 7);
    //显示效果图
    image3s = cvMatToQPixmap(image2s);
    image3s = image3s.scaled(ui.label->width(), ui.label->height());
    ui.label->setPixmap(image3s);
}

//Laplace算子边缘检测
void MyQtAndCProjectWithVS::button6() 
{
    //载入原始图
    Mat srcImage10 = QPixmapToCvMat(QPixmap::fromImage(currentimage));
    Mat dstImage4, grayImage4, abs_dstImage, gaussImage;
    QPixmap dstImages4;
    //转换为灰度图
    cvtColor(srcImage10, grayImage4, COLOR_RGB2GRAY);
    //高斯滤波消除噪声
    GaussianBlur(grayImage4, gaussImage, 
        Size(kenelValue * 2 + 1, kenelValue * 2 + 1), 0, 0, BORDER_DEFAULT);
    //Laplacian算子边缘检测
    Laplacian(gaussImage, abs_dstImage, CV_16S, 3, 1, 0, BORDER_DEFAULT);
    //求绝对值
    convertScaleAbs(abs_dstImage, dstImage4);
    //显示效果图
    dstImages4 = cvMatToQPixmap(dstImage4);
    dstImages4 = dstImages4.scaled(ui.label->width(), ui.label->height());
    ui.label->setPixmap(dstImages4);
}

MyQtAndCProjectWithVS::~MyQtAndCProjectWithVS()
{}
